//
//  SearchMsgResultCell.m
//  ChangChat
//
//  Created by Cocoa on 2019/10/17.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "SearchMsgResultCell.h"
#import "PPStickerDataManager.h"
#import "NSAttributedString+PPAddition.h"
#import "NSDate+Debug.h"

@interface SearchMsgResultCell ()

@property (nonatomic, strong) MASConstraint *msgTextViewHeightLayout;


@end
@implementation SearchMsgResultCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    self.iconImgView = UIImageView.new;
    self.nameLabel = [UILabel k_labelWithFontSize:15 textColor:UIColor.k_t2Color];
    self.timeLabel = [UILabel k_labelWithFontSize:12 textColor:UIColor.k_t4Color];
    
    self.contentTextView = [YYTextView new];
    self.contentTextView.backgroundColor = UIColor.clearColor;
    self.contentTextView.editable = NO;
//    self.contentTextView.delegate = self;
    self.contentTextView.dataDetectorTypes = UIDataDetectorTypeLink;
    //    self.msgTextLabel.cancelsTouchesInView = NO;
//    self.msgContentViewTapGR.cancelsTouchesInView = NO;
    self.contentTextView.selectable = NO;
    self.contentTextView.selectable = YES;
    //    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //    paragraphStyle.lineSpacing = 3;
    //    paragraphStyle.
    self.contentTextView.linkTextAttributes = @{NSForegroundColorAttributeName : UIColor.k_hexColor(0x4BDFF3),
                                             NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle]
                                             };
    self.contentTextView.contentInset = UIEdgeInsetsZero;
    self.contentTextView.textContainerInset = UIEdgeInsetsZero;
    
    [self.contentView addSubview:self.iconImgView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.contentTextView];
    
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    self.iconImgView.layer.cornerRadius = 25.f;
    self.iconImgView.layer.masksToBounds = YES;
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImgView.mas_right).offset(10);
        make.right.equalTo(self.timeLabel.mas_left).offset(-10);
        make.top.equalTo(self.iconImgView).offset(5);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.equalTo(self.nameLabel);
    }];
    [self.timeLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [self.contentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.right.mas_offset(-15);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(5);
        make.bottom.mas_lessThanOrEqualTo(-15);
        self.msgTextViewHeightLayout = make.height.mas_equalTo(30);
    }];
    
    
}


- (void)setMsg:(MsgItem *)msg {
    _msg = msg;
    
    [kUsermanager getFriendWithUserId:msg.send_user_id groupId:msg.group_id callbackBlock:^(FriendItem * _Nonnull friendItem) {
        [self.iconImgView sd_setImageWithURL:friendItem.avatar.toURL placeholderImage:UIImage.k_imgName(@"placeholder_user")];
        self.nameLabel.text = friendItem.displayName;
    }];
    self.timeLabel.text = [NSDate yyyy_MM_ddOrMM_ddWithTimestamp:msg.createtime];
    
    
    
    NSDictionary *attDic = @{ NSFontAttributeName: kUsermanager.msgFont, NSForegroundColorAttributeName: UIColor.k_t2Color };
    NSMutableAttributedString *attributedComment = [[NSMutableAttributedString alloc] initWithString:msg.content?:@"" attributes:attDic];
    if (self.keyword.isHaveStr == YES) {
        NSString *regexStr = [NSString stringWithFormat:@"/?(%@)", self.keyword];
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:nil];
        
        NSArray *matches = [regex matchesInString:msg.content options:0 range:NSMakeRange(0,msg.content.length)];
        
        NSDictionary *attDic = @{NSForegroundColorAttributeName: UIColor.k_t1Color};
        for(NSTextCheckingResult *result in [matches objectEnumerator]) {
            
            NSRange range = [result range];
            [attributedComment addAttributes:attDic range:range];
        }
    }
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 3;
    [attributedComment addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:attributedComment.pp_rangeOfAll];
    [[PPStickerDataManager sharedInstance] replaceEmojiForAttributedString:attributedComment font:kUsermanager.msgFont];
    CGFloat msgWidht = ScreenWidth - 15*2 - 50 - 10;
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:CGSizeMake(msgWidht, CGFLOAT_MAX) text:attributedComment];
//    self.msgTextViewWidthLayout.offset(layout.textBoundingSize.width);
    self.msgTextViewHeightLayout.offset(layout.textBoundingSize.height);
    self.contentTextView.attributedText = attributedComment;
    
}

@end
